package com.easy.generator.vue;

import com.easy.generator.Easy;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.time.LocalDate;
import java.util.Set;

/**
 * @author easy
 * @date 2025年07月11日 16:19
 */
public class API_Generator {
    private static final String DIR_NAME = Easy.VUE_LOCAL_PATH;
    private static final String DEST_PATH = Easy.VUE_LOCAL_PATH + "/src/api";


    public static void generateAPI() throws Exception {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        // 扫描包下的所有类
        Set<Class<?>> classes = Easy.scanClasses(Easy.BEAN_PACKAGE, classLoader);
        // 为每个实体类生成Controller
        for (Class<?> clazz : classes) {
            String className = clazz.getSimpleName();
            String varName = className.toLowerCase();
            if (Easy.isEntityClass(clazz)) {
               String filecontent="import http from '../util/request.js';\n" +
                       "\n" +
                       "export default {\n" +
                       "\n" +
                       "    one:(id)=>{\n" +
                       "        return http({\n" +
                       "            url:'api/"+varName+"/'+id,\n" +
                       "            method:'get'\n" +
                       "        });\n" +
                       "    },\n" +
                       "    page:(params)=>{\n" +
                       "        return http({\n" +
                       "            url:'api/"+varName+"/page',\n" +
                       "            method:'get',\n" +
                       "            params:params\n" +
                       "        });\n" +
                       "    },\n" +
                       "    all:(params)=>{\n" +
                       "        return http({\n" +
                       "            url:'api/"+varName+"',\n" +
                       "            method:'get',\n" +
                       "            params:params\n" +
                       "        });\n" +
                       "    },\n" +
                       "    add:(obj)=>{\n" +
                       "        return http({\n" +
                       "            url:'api/"+varName+"',\n" +
                       "            method:'post',\n" +
                       "            data: obj,\n" +
                       "            headers: {\n" +
                       "                'Content-Type': 'application/json'\n" +
                       "            }});\n" +
                       "    },\n" +
                       "    delete:(id)=>{\n" +
                       "        return http({\n" +
                       "            url:\"api/"+varName+"/\"+id,\n" +
                       "            method:'delete'\n" +
                       "        });\n" +
                       "    },\n" +
                       "    edit:(obj)=>{\n" +
                       "        return http({\n" +
                       "            url:\"api/"+varName+"\",\n" +
                       "            method:'PUT',\n" +
                       "            data: obj,\n" +
                       "            headers: {\n" +
                       "                'Content-Type': 'application/json'\n" +
                       "            }\n" +
                       "        });\n" +
                       "    },\n" +
                       "}\n";

                String content = String.format(filecontent, LocalDate.now());
                File outputFile = new File(DEST_PATH + "/" + varName+".js");
                if (!outputFile.exists()) {
                    outputFile.getParentFile().mkdirs();
                    outputFile.createNewFile();
                }
                try (FileWriter writer = new FileWriter(outputFile)) {
                    writer.write(content);
                }
                System.out.println("已生成文件：" + outputFile.getAbsolutePath());
            }
        }
    }

    private static final String PACKAGE_NAME = Easy.MODEL_PACKAGE+".generator.vue.file";
    private static final String FILE_LOGIN_NAME = "login.js";
    public static void generateLogin() throws IOException {
        File srcFile = new File(Easy.PROJECT_ABSOLUTE_URL + Easy.SRC_DIR + Easy.convertPackagePath(PACKAGE_NAME) + "/"+FILE_LOGIN_NAME );
        File destFile = new File(DEST_PATH +"/"+ FILE_LOGIN_NAME);
        Easy.mkdirs(DEST_PATH) ;
        Files.copy(srcFile.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        System.out.println("已生成文件：---" + destFile.getAbsolutePath());
    }

    private static final String FILE_CAPTCHA_NAME = "captcha.js";
    public static void generateCaptcha() throws IOException {
        File srcFile = new File(Easy.PROJECT_ABSOLUTE_URL + Easy.SRC_DIR + Easy.convertPackagePath(PACKAGE_NAME) + "/"+FILE_CAPTCHA_NAME );
        File destFile = new File(DEST_PATH +"/"+ FILE_CAPTCHA_NAME);
        Easy.mkdirs(DEST_PATH) ;
        Files.copy(srcFile.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        System.out.println("已生成文件：---" + destFile.getAbsolutePath());
    }
    public static void generate() throws Exception {
        generateAPI();
        generateLogin();
        generateCaptcha();
    }

}
